// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import Qt5Compat.GraphicalEffects
import QtQuick.Controls
import Fk
import Fk.Components.Common
import Fk.Widgets as W

Item {
  id: root
  property alias skill: skill.text
  property string type: "active"
  property string tag: ""
  property string orig: ""
  property bool pressed: false
  property bool doubleTapped: false
  property bool prelighted: false // 是否已预亮
  property bool locked: false // 是否被无效
  property bool is_wide: { // 宽按钮
    if (forceShort) return false;
    if (skill.text.length > 3) return true;
    if (oddIndex) return true;
    return false
  }
  property int times: -1

  property bool isActiveSkill: false // 是否是主动技
  property bool isFakeSkill: false // 是否是可预亮技
  property bool toggleAble: false // 是否可默认发动
  property bool toggled: false // 是否默认发动
  property string buttonStyle: "frequent"
  property bool oddIndex: false // 是否在短技能的最后一个奇数index
  property bool forceShort: false
  property bool hovered: false

  onEnabledChanged: {
    if (!enabled) {
      pressed = false;
      toggled = false;
    }
  }

  onOrigChanged: {
  // function foo(){
    const [ style, isActive, isFake ] = Lua.evaluate(`(function()
      local skill = Fk.skills["${orig}"]
      local style = "frequent"
      local isActive = false
      if not skill then return { style, isActive } end
      if skill:isInstanceOf(ActiveSkill) or skill:isInstanceOf(ViewAsSkill) then
        isActive = true
      end

      if skill:hasTag(Skill.Wake) then
        style = "wake"
      elseif skill:hasTag(Skill.Limited) then
        style = "limited"
      elseif skill:hasTag(Skill.Compulsory) then
        style = "compulsory"
      elseif isActive then
        style = "normal"
      end

      local isFake = Self:isFakeSkill(skill) and not isActive
      return { style, isActive, isFake }
    end)()`);

    buttonStyle = style;
    isActiveSkill = isActive;
    isFakeSkill = isFake;
    toggleAble = (style === "frequent" && !isFakeSkill);
    enabled = (style === "frequent" || isFakeSkill);
  }

  width: buttonBg.width
  height: buttonBg.height

  Image {
    id: buttonBg
    width: sourceSize.width * 0.85
    height: sourceSize.height * 0.85
    source: {
      let prefix = root.is_wide ? "1" : "2";
      let suffix = "-disabled";
      if (root.isActiveSkill) {
        if (root.enabled) {
          suffix = (pressed ? "-down" : (hovered ? "-hover" : "-normal"));
        }
      } else {
        // 若为发动后的觉醒技则显示为normal
        // TODO 需要修改UpdateLimitSkill 否则不刷新
        const waked = !!Lua.evaluate(`(function()
          local skill = Fk.skills["${orig}"]
          return skill and skill:hasTag(Skill.Wake) and
            Self:usedSkillTimes("${orig}", Player.HistoryGame) > 0
        end)()`);
        if (waked) {
          suffix = "-normal";
        } else {
          suffix = toggleAble ? (toggled ? "-down" : (hovered ? "-hover" : "-normal")) : "-disabled";
        }
      }
      let ret = Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/skill/" + root.buttonStyle + "/";
      return ret + prefix + suffix;
    }
  }

  Rectangle {
    id: buttonBgShadow
    anchors.fill: buttonBg
    radius: 2
    visible: root.locked
    color: "black"
    opacity: 0.4
  }

  Rectangle {
    id: buttonUnprelightedShadow
    anchors.fill: buttonBg
    radius: 2
    visible: root.isFakeSkill && !root.prelighted
    color: "black"
    opacity: 0.4
  }

  Text {
    id: skill
    x: (parent.width - width + 11) / 2
    // topPadding: 5
    anchors.verticalCenter: parent.verticalCenter
    color: root.locked ? '#666666' : "white"
    // font.family: "simkai, 楷体, KaiTi, 楷体_GB2312, KaiTi_GB2312"
    font.family: "KaiTi_GB2312"
    font.pixelSize: 12 // Math.max(16 - text.length, 13)
    font.bold: true
    // visible: false

    style: Text.Outline
    styleColor: "#bc000000"
  }

  Image {
    property string imgName: root.is_wide ? "skill-locked-wide" : "skill-locked"
    source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/" + imgName + ".png"
    z: 2
    visible: root.locked
    anchors.fill: parent
    anchors.topMargin: -1
    anchors.bottomMargin: -2
    fillMode: Image.PreserveAspectFit
  }

  Item {
    width: 12
    height: 12
    visible: root.times > -1
    anchors.right: parent.right
    anchors.rightMargin: -1
    anchors.top: parent.top
    anchors.topMargin: -1

    Image {
      source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/skill-times-bg.png"
      anchors.fill: parent
    }

    Text {
      id: count
      anchors.centerIn: parent
      font.pixelSize: 10
      font.bold: true
      color: skill.color
      font.family: "SimSun"
      style: Text.Outline
      styleColor: "black"
      text: root.times
      z: 1.5
    }

  }

  W.TapHandler { // 前提：root.enabled
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    onTapped: (p, btn) => {
      if (btn === Qt.LeftButton || btn === Qt.NoButton) {
        if (root.isActiveSkill || root.isFakeSkill) {
          parent.pressed = !parent.pressed;
        } else if (root.toggleAble) {
          parent.toggled = !parent.toggled;
        }
      } else if (btn === Qt.RightButton) {
        skillDetail.visible = true;
      }
    }
  }

  HoverHandler {
    id: hover
    cursorShape: root.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
    onHoveredChanged: {
      if (hovered) {
        root.hovered = true;
        longHoverTimer.start();
      } else {
        root.hovered = false;
        longHoverTimer.stop();
        skillDetail.visible = false;
      }
    }
  }

  Timer {
    id: longHoverTimer
    interval: 400
    running: false
    repeat: false
    onTriggered: {
      if (hover.hovered) {
        skillDetail.visible = true;
      }
    }
  }

  Rectangle {
    id: skillDetail
    anchors.left: parent.left
    anchors.leftMargin: -2
    anchors.bottom: parent.top
    anchors.bottomMargin: 2
    width: 200
    height: Math.max(detailText.contentHeight + 14, 60)
    color: "transparent"
    visible: false

    Rectangle {
      anchors.fill: parent
      color: '#e01d1d1d'
      radius: 1
    }

    Text{
      id: detailText
      width: parent.width
      text: "<b><font size='5'>" + Lua.tr(orig) + "</font><br> " + Lua.tr(":" + orig) + "</b>"
      font.pixelSize: 10
      font.family: "SimSun"
      wrapMode: Text.WordWrap
      textFormat: TextEdit.RichText
      color: '#ffefb7'
      anchors.fill: parent
      anchors.margins: 8
    }

    BorderImage {
      source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/skill/skill-detail-frame.png"
      border.left: 25
      border.right: 25
      border.top: 25
      border.bottom: 25
      anchors.fill: parent
      anchors.margins: -1
    }
  }
}
